﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Serialization;
using haptic.src.engine.core;
using Microsoft.Xna.Framework.Content;
using haptic.src.engine.resource;

namespace haptic.src.engine.sprite
{
    [Serializable]
    public class SpriteData : Resource
    {
        #region Constructors
        public SpriteData()
        {

        }
        public SpriteData(string sFilePath) : base(sFilePath)
        {
        }
        #endregion

        #region Edit
        private void Build()
        {
            Debug.Assert(m_uiFrameCount > 0);
            m_fFrameDelay = fTotalDuration / m_uiFrameCount;
            if (m_oTexture == null)
            {
                m_fFrameWidth = 0.0f;
                m_fFrameHeight = 0.0f;
                m_fSquaredRadius = 0.0f;
            }
            else
            {
                m_fFrameWidth = m_oTexture.Width / m_uiFrameCount;
                m_fFrameHeight = m_oTexture.Height;
                m_fSquaredRadius = m_fFrameWidth * m_fFrameWidth + m_fFrameHeight * m_fFrameHeight;
            }
        }
        #endregion

        #region Load
        public override Result PostLoad(ContentManager oContent)
        {
            Debug.Assert(oContent != null);
            if (m_sTexturePath == null)
                return new Result("No texture path was specified for the SpriteData : " + m_sFilePath);
            m_oTexture = (Texture2D)oContent.Load<Texture2D>(m_sTexturePath);
            if (m_oTexture == null)
                return new Result("Cannot load the texture in the SpriteData : " + m_sTexturePath);
            Build();
            return Result.Success;
        }
        public override void Unload()
        {
            if (m_oTexture != null)
            {
                m_oTexture.Dispose();
                m_oTexture = null;
            }
            base.Unload();
        }
        #endregion

        #region Attributes
        [XmlAttribute("texture-path")]
        public string m_sTexturePath;
        [NonSerialized]
        private Texture2D m_oTexture;
        public Texture2D oTexture { get { return m_oTexture; } }
        private float m_fFrameWidth = 0.0f;
        public float fFrameWidth { get { return m_fFrameWidth; } }
        private float m_fFrameHeight = 0.0f;
        public float fFrameHeight { get { return m_fFrameHeight; } }
        [XmlAttribute("frame-count")]
        public uint m_uiFrameCount = 1;
        private float m_fTotalDuration = 0.0f;
        [XmlAttribute("total-duration")]
        public float fTotalDuration { get { return m_fTotalDuration; } set { m_fTotalDuration = value; m_fInvTotalDuration = m_fTotalDuration == 0.0f ? 0.0f : 1.0f / m_fTotalDuration; } }
        private float m_fInvTotalDuration = 0.0f;
        [XmlIgnore]
        public float fInvTotalDuration { get { return m_fInvTotalDuration; } }
        private float m_fFrameDelay = 0.0f;
        public float fFrameDelay { get { return m_fFrameDelay; } }
        private float m_fSquaredRadius = 0.0f;
        public float fSquaredRadius { get { return m_fSquaredRadius; } }
        public float fRadius { get { return m_fSquaredRadius * m_fSquaredRadius; } }
        #endregion
    }
}
